package com.example.videoworks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.videoworks.common.Page;
import com.example.videoworks.common.PageParam;
import com.example.videoworks.controller.request.GetListRequest;
import com.example.videoworks.entity.Principal;
import com.example.videoworks.entity.Work;
import com.example.videoworks.mapper.PrincipalMapper;
import com.example.videoworks.mapper.WorkMapper;
import com.example.videoworks.service.IWorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lily
 * @since 2023-05-11
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements IWorkService {

    @Autowired
    private WorkMapper workMapper;
    @Override
    public Page<Work> getWorkById(GetListRequest request, String id) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("workId", id);
        List<Work> list = workMapper.selectList(queryWrapper);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Work getWork(String id, Integer ordering) {
        QueryWrapper<Work>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("workId", id);
        queryWrapper.eq("ordering", ordering);
        Work work = workMapper.selectOne(queryWrapper);
        return work;
    }

    @Override
    public Page<Work> findWorkByTitle(GetListRequest request, String title) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.searchTitle(title);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Page<Work> rateOneTypeWork(GetListRequest request, String type) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.rankOneTypeWorkRating(type);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Page<Work> rateOneTypeWorkByStartyear(GetListRequest request, String type) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.rankOneTypeWorkByStartyear(type);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Page<Work> searchOnType(GetListRequest request, String searchParam, String type) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.searchTitleOnType(searchParam, type);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Page<Work> filterType(GetListRequest request, String type) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.filterType(type);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Page<Work> filterGenre(GetListRequest request, String genre) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.filterGenre(genre);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Page<Work> filterRegion(GetListRequest request, String region) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.filterRegion(region);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Page<Work> advancedSearch(GetListRequest request, String searchParam, String lowerYear, String upperYear, String type, String region, String genre) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.advanceSearch(searchParam, lowerYear, upperYear, type, region, genre);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Page<Work> SearchOrderRating(GetListRequest request, String searchParam, String lowerYear, String upperYear, String type, String region, String genre) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.SearchOrderRating(searchParam, lowerYear, upperYear, type, region, genre);
        return new Page(new PageInfo<>(list));
    }

    @Override
    public Page<Work> SearchOrderYear(GetListRequest request, String searchParam, String lowerYear, String upperYear, String type, String region, String genre) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序
        List<Work> list = workMapper.SearchOrderYear(searchParam, lowerYear, upperYear, type, region, genre);
        return new Page(new PageInfo<>(list));
    }


}
